import CryptoJS from 'crypto-js'
import JSEncrypt from 'jsencrypt'
import qs from 'qs';

const aes_iv = "lLBvDbxgqUStnz87";
const aes_key = "d22icaUY3o9NpQM0";
const publicKey = '-----BEGIN PUBLIC KEY-----\n' +
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGCIMAF5RmMqyEeA07AqxBsn43\n' +
    'JLS1NQyjE/ETmvlYmF6n6sZIe9BtMDVT0GfDQKgXvQzAEpgJVmDma6ekM9KrgRju\n' +
    'EpigzmrogJxHSgh+CgnAv3tWRVAJF34ZvrHj0T7Rp8M468tFrCikh2y1cuIkL+eS\n' +
    'g/gzaav0gdz7eudP6wIDAQAB\n' +
    '-----END PUBLIC KEY-----';
const priKey = '';

const func = {
    /**
     * json 排序
     * 先排序再toUpperCase
     */
    getSign(jsonObj) {
        const arr = [];
        for (var key in jsonObj) {
            arr.push(key)
        }
        arr.sort();
        var str = '&';
        for (var i in arr) {
            str += arr[i] + '=' + jsonObj[arr[i]] + '&';
        }
        str = str.slice(1, -1);
        var aes_res = this.aes_encrypt(str);
        return aes_res;
    },
    postSign(jsonObj) {
        // 加密时解码
        jsonObj = qs.parse(jsonObj);
        // console.log(jsonObj)
        let arr = [];
        Object.keys(jsonObj).forEach((key) => {
            arr.push(key)
        });
        arr.sort();
        let str = '&';
        for (var i in arr) {
            var val = jsonObj[arr[i]]
            if (typeof jsonObj[arr[i]] == 'object') {
                var val = Object.values(jsonObj[arr[i]]);
            }
            str += arr[i] + '=' + val + '&';
        }
        str = str.slice(1, -1);
        var aes_res = this.aes_encrypt(str);
        return aes_res;
    },
    aes_encrypt(str) {
        var key = CryptoJS.enc.Utf8.parse(aes_key);
        var iv = CryptoJS.enc.Utf8.parse(aes_iv);
        var encrypted = CryptoJS.AES.encrypt(str, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }).ciphertext.toString();
        return encrypted;
    },
    //解密
    aes_decrypt(str) {
        var key = CryptoJS.enc.Utf8.parse(aes_key);
        var iv = CryptoJS.enc.Utf8.parse(aes_iv);
        var encryptedHexStr = CryptoJS.enc.Hex.parse(str);
        var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
        var decrypt = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypt.toString(CryptoJS.enc.Utf8);
    },
    // rsa 加密
    RSAencrypted(data) {
        // 新建JSEncrypt对象
        let encryptor = new JSEncrypt();
        // 设置公钥
        encryptor.setPublicKey(publicKey);
        return encodeURI(encryptor.encrypt(data));

    },
    RSAdecrypted(data) {
        // 新建JSEncrypt对象
        let encryptor = new JSEncrypt();
        // 设置公钥
        encryptor.setPrivateKey(priKey);
        return encryptor.decrypt(data);
    },
    toArrayBuffer(buf) {
        var ab = new ArrayBuffer(buf.length);
        var view = new Uint8Array(ab);
        for (var i = 0; i < buf.length; ++i) {
            view[i] = buf[i];
        }
        return ab;
    },
    toBuffer(ab) {
        var buf = new Buffer(ab.byteLength);
        var view = new Uint8Array(ab);
        for (var i = 0; i < buf.length; ++i) {
            buf[i] = view[i];
        }
        return buf;
    }
};
export default func